#!/usr/bin/php
<?php

# Comment out to allow the ability to run from web
#if (PHP_SAPI !== 'cli') exit("Not CLI");

/*
* This is a cron job to run every 5 minutes and check for any expired labels that have been checked 
* out but never returned.  Anything over 20 minutes will be considered lost and repopulated 
* for someone else to data entry.
*/

#ALTER TABLE `birdcenter`.`queue_occurrence` ADD UNIQUE `uniqueKey` ( `occurrenceId` , `imageId` ) 

$minutes = 20;
$expireDate = mktime() - ($minutes * 60);
					 
$query = 'SELECT * FROM `occurrence` WHERE historicalCard = 1 AND deleted = 0 AND stage = "Queue" AND check_out != "0000-00-00 00:00:00" AND check_in = "0000-00-00 00:00:00" AND UNIX_TIMESTAMP(check_out) < ' . $expireDate . ';';

error_reporting(E_ALL ^ E_NOTICE);
ini_set('display_errors', '1');
include_once('../../config.php');

require_once( $config['basePath'] . 'resources/api/class.dbconnect.php');
require_once( $config['basePath'] . 'resources/api/class.lsu.php');
require_once('class.speciesoccurrence.php');
$occ = new Lsu;
$spOcc = new SpeciesOccurrence();
$i=0;

$db = new DbConnect(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME);
$db->open();
#print $query;
$result = mysql_query($query);

if (!$result) {
	die($message);
}

while ($row = mysql_fetch_assoc($result)) {
	$i++;
	$spOcc->load_by_id($row["id"]);
	$spOcc->set('check_out', "0000:00:00 00:00:00");
	$spOcc->save(1);

	$occ->queueOccurrence->set('occurrenceId', $row["id"]);
	$occ->queueOccurrence->set('imageId', $row["imageId"]);
	$occ->queueOccurrence->save();			
}

print "<br>Total Updated: $i";
?>